<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * 配送员管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['groups']=pdo_getall('jy_peimilk_level',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>0),array('id','title'),'id');
$return['groups'][0]=array('id'=>0,'title'=>'默认等级');
$return['ops']=array(
	'1'=>'订单管理',
	'2'=>'接收订单通知',
	'3'=>'服务员通知',
	'4'=>'叫号管理',
);
if($_RQ['op']=='list'){
	$params=array();
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile like']='%'.trim($_RQ['mobile']).'%';
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if(!empty($_RQ['mid'])){
		$params['mid']=$_RQ['mid'];
		$return['params']['mid']=$_RQ['mid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','title','mobile','online','status','level_id','merchid','area_id','mid');
	$list=pdo_getslice('jy_peimilk_member',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
		$item['areatitle']=pdo_getcolumn('jy_peimilk_area',array('id'=>$item['area_id'],'uniacid'=>$_SESSION['uniacid']),'title');
		$item['storetitle']=pdo_getcolumn('jy_merch_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']),'merchname');
		$item['orderstatus2']=pdo_count('jy_weishop_order',array('peisong_id'=>$item['mid'],'status'=>2,'uniacid'=>$_SESSION['uniacid']),'id');
		$item['orderstatus3']=pdo_count('jy_weishop_order',array('peisong_id'=>$item['mid'],'status'=>3,'uniacid'=>$_SESSION['uniacid']),'id');
		$item['noworderstatus2']=pdo_count('jy_weishop_order',array('peisong_id'=>$item['mid'],'createtime'=>$daytime,'status'=>2,'uniacid'=>$_SESSION['uniacid']),'id');
		$item['noworderstatus3']=pdo_count('jy_weishop_order',array('peisong_id'=>$item['mid'],'createtime'=>$daytime,'status'=>3,'uniacid'=>$_SESSION['uniacid']),'id');
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'配送员列表'),
			array('op'=>'edit','title'=>'添加配送员'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：配送员，在线，离线，忙碌状态，只允许配送员自己手机端设置！离线，未审核的，将不派单，也无法指定配送员！</br>
		只要存在待接收的订单，就会响一次。同一订单只响一次，除非刷新页面。
		</br>只要系统指派，后台指定，这种情况，才会出现待接收的订单。自己抢的单不算。
		','list'=>array(
			array('name'=>'mid','title'=>'MID','type'=>'string'),
			array('name'=>'title','title'=>'配送员姓名','type'=>'string'),
			array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
			array('name'=>'mobile','title'=>'联系电话','type'=>'string'),
			array('name'=>'online','title'=>'在线','type'=>'radio','options'=>'onlines'),
			array('name'=>'status','title'=>'审核','type'=>'switch','set'=>'set'),
			array('name'=>'storetitle','title'=>'所属商户','type'=>'string'),
			array('name'=>'areatitle','title'=>'所属区域','type'=>'string'),
			array('type'=>'select','title'=>'配送等级','name'=>'level_id','options'=>'groups'),
			array('type'=>'img_string','title'=>'总完成/配送中订单',
			'data'=>array(
				array('title'=>'完成订单数：','name'=>'orderstatus2'),
				array('title'=>'配送中订单数：','name'=>'orderstatus3')
				)
			),
			array('type'=>'img_string','title'=>'今日完成/配送中订单',
			'data'=>array(
				array('title'=>'完成订单数：','name'=>'noworderstatus2'),
				array('title'=>'配送中订单数：','name'=>'noworderstatus3')
				)
			),
		),'action'=>array(
			array('type'=>'href','op'=>'biao','title'=>'今日配送表'),
			array('type'=>'href','op'=>'biao2','title'=>'明日配送表'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'配送员姓名','type'=>'string'),
			array('name'=>'mid','width'=>15,'title'=>'配送员MID','type'=>'string'),
			array('name'=>'mobile','width'=>15,'title'=>'联系电话','type'=>'string'),
		)
	);
	$return['onlines']=array('离线','在线','忙碌');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择配送员！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['status'])){
		$update['status']=intval($post['status']);
	}
	if($update){
		pdo_update('jy_peimilk_member',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_peimilk_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$item['pcd']=json_decode($item['pcd']);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回配送员列表'),
		),
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'配送员姓名','name'=>'title','help'=>''),
					array('type'=>'string','title'=>'联系电话','name'=>'mobile','help'=>''),
					array('type'=>'choose','title'=>'关联会员','name'=>'mid','help'=>'','plugin'=>'core','action'=>'member.get_member','choose_title'=>'选择会员'),
					array('type'=>'pcd','title'=>'区域地址','name'=>'pcd','help'=>''),
					array('type'=>'select','title'=>'所属配送点','name'=>'area_id','help'=>'','options'=>'areas'),
					array('type'=>'select','title'=>'配送等级','name'=>'level_id','help'=>'','options'=>'groups'),
					array('type'=>'text','title'=>'备注','name'=>'desc','help'=>''),
					array('type'=>'switch','title'=>'状态','name'=>'status','help'=>''),
				)
			),
		)
	);
	$return['areas']=pdo_getall('jy_peimilk_area',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>0));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'mobile'=>trim($postdata['mobile']),
		'level_id'=>intval($postdata['level_id']),
		'area_id'=>intval($postdata['area_id']),
		'mid'=>intval($postdata['mid']),
		'desc'=>trim($postdata['desc']),
		'status'=>intval($postdata['status']),
		'pcd'=>ijson_encode($postdata['pcd']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_peimilk_member',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_peimilk_member',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_peimilk_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='biao' || $_RQ['op']=='biao2'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_peimilk_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));		
	}else{
		$item=array();
	}
	$day=$_RQ['op']=='biao'?date('Y-m-d'):date('Y-m-d',time()+24*3600);
	$params=array('day'=>$day,'sendtype'=>1,'status'=>0,'mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']);
	$sendlogs=pdo_getall('jy_peimilk_sendlog',$params);
	$daygoods=array();
	$goodsnum=0;
	if($sendlogs){
		foreach($sendlogs as &$sv){
			$peisetgoods=json_decode($sv['goods'],true);
			foreach($peisetgoods as $key=>$gv){
				$gv['unit']=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),'unit');
				$goodsnum +=$gv['num'];
				if($daygoods['goods'][$key]){
					$daygoods['goods'][$key]['num'] +=$gv['num'];
					$daygoods['goods'][$key]['lognum'] ++;
				}else{
					$gv['lognum']=1;
					$gv['name']=$item['title'];
					if($gv['selespename']){
						$gv['leititle'] .='('.$gv['selespename'].')';
					}
					$daygoods['goods'][$key]=$gv;
				}
			}
		}
	}
	$return['daylognum']=count($sendlogs);
	$return['goodsnum']=$goodsnum;
	$return['list']=$daygoods['goods'];
	$day=$_RQ['op']=='biao'?'今日':'明日';
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'name','title'=>'配送员姓名','type'=>'string'),
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'leititle','title'=>'类型/套餐','type'=>'string'),
			array('name'=>'num','title'=>$day.'待配总数','type'=>'string'),
			array('name'=>'lognum','title'=>$day.'待配单数','type'=>'string'),
		))
	);
	$return['htmls']=$htmls;
	exi($return);
}